package com.ruoyi.safety.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.safety.domain.Device;
import com.ruoyi.safety.dto.DeviceDto;
import com.ruoyi.safety.mapper.DeviceMapper;
import com.ruoyi.safety.service.IDeviceService;
import com.ruoyi.safety.vo.DeviceVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import java.util.ArrayList;
import java.util.List;

/**
 * 设备Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-11
 */
@Service
public class DeviceServiceImpl implements IDeviceService 
{
    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询设备
     * 
     * @param deviceId 设备主键
     * @return 设备
     */
    @Override
    public Device selectDeviceByDeviceId(String deviceId)
    {
        return deviceMapper.selectDeviceByDeviceId(deviceId);
    }

    /**
     * 查询设备列表
     * 
     * @param device 设备
     * @return 设备
     */
    @Override
    public List<Device> selectDeviceList(Device device)
    {
        return deviceMapper.selectDeviceList(device);
    }

    /**
     * 新增设备
     * 
     * @param device 设备
     * @return 结果
     */
    @Override
    public int insertDevice(Device device)
    {
        return deviceMapper.insertDevice(device);
    }

    /**
     * 修改设备
     * 
     * @param device 设备
     * @return 结果
     */
    @Override
    public int updateDevice(Device device)
    {
        return deviceMapper.updateDevice(device);
    }

    /**
     * 修改设备权限清单
     *
     * @param deviceVo 设备
     * @return 结果
     */
    @Override
    public int updateDeviceAuth(DeviceVo deviceVo)
    {
        String[] deviceArr = deviceVo.getDeviceIds();
//        Device device = new Device();
//        if (deviceArr != null){
//            for (String deviceId : deviceArr) {
//                device.setDeviceId(deviceId);
//                device.setAuthUserName(deviceVo.getAuthUserName());
//                return deviceMapper.updateDeviceAuth(device);
//            }
//        }
        //device.setAuthUserName(deviceVo.getAuthUserName());
        String authUserName = deviceVo.getAuthUserName();
        return deviceMapper.updateDeviceAuth(deviceArr,authUserName);
    }

    /**
     * 批量删除设备
     * 
     * @param deviceIds 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteDeviceByDeviceIds(String[] deviceIds)
    {
        return deviceMapper.deleteDeviceByDeviceIds(deviceIds);
    }

    /**
     * 删除设备信息
     * 
     * @param deviceId 设备主键
     * @return 结果
     */
    @Override
    public int deleteDeviceByDeviceId(String deviceId)
    {
        return deviceMapper.deleteDeviceByDeviceId(deviceId);
    }

    @Override
    public List<DeviceDto> selectDeviceVoList(DeviceVo deviceVo) {;
        //通过传递的值查询device的值
        List<DeviceDto> deviceDtoList = deviceMapper.selectDeviceVoList(deviceVo);
        for (DeviceDto deviceDto : deviceDtoList) {
            //品牌
            if (StringUtils.isNotBlank(deviceDto.getBrand())){
                SysDictData brand1 = sysDictDataMapper.selectSysDictItemByDictCodeAndItemValue("brand", deviceDto.getBrand());
                if (brand1 != null){
                    deviceDto.setBrandDictText(brand1.getDictLabel());
                }
            }
            //是否支持通话
            if (deviceDto.getIsSupportCalling() != null){
                SysDictData isSupportCallingDictText = sysDictDataMapper.selectSysDictItemByDictCodeAndItemValue("is_support_calling", deviceDto.getIsSupportCalling().toString());
                if (isSupportCallingDictText != null) {
                    deviceDto.setIsSupportCallingDictText(isSupportCallingDictText.getDictLabel());
                }
            }
            //是否启用
            if (deviceDto.getEnable() != null){
                SysDictData enableDictText = sysDictDataMapper.selectSysDictItemByDictCodeAndItemValue("enable", deviceDto.getEnable().toString());
                if (enableDictText != null) {
                    deviceDto.setEnableDictText(enableDictText.getDictLabel());
                }
            }
            //状态
            if(deviceDto.getCameraStatus() != null){
                SysDictData enableDictText = sysDictDataMapper.selectSysDictItemByDictCodeAndItemValue("status", deviceDto.getCameraStatus());
                if (enableDictText != null) {
                    deviceDto.setCameraStatusDictText(enableDictText.getDictLabel());
                }
            }

        }

        return deviceDtoList;
        }

    /**
     * 查询设备
     *
     * @param deviceId 设备主键
     * @return 设备
     */
    @Override
    public DeviceDto selectDeviceDtoByDeviceId(String deviceId) {
        DeviceDto deviceDto = deviceMapper.selectDeviceDtoByDeviceId(deviceId);
//        if (deviceDto.getResponsibleDepartmentId() != null && !deviceDto.getResponsibleDepartmentId().equals("")){
//            SysDept sysDept = sysDeptMapper.selectDeptById(Long.parseLong(deviceDto.getResponsibleDepartmentId()));
//            if (sysDept != null) {
//                deviceDto.setResponsibleDepartmentName(sysDept.getDeptName());
//            }
//        }
//        if (deviceDto.getResponsiblePersonId() != null && !deviceDto.getResponsiblePersonId().equals("")){
//            SysUser user = sysUserMapper.selectUserById(Long.parseLong(deviceDto.getResponsiblePersonId()));
//            if (user != null) {
//                deviceDto.setResponsiblePersonName(user.getNickName());
//            }
//        }

        return deviceDto;
    }

}
